//封装方法
export function getUrlParam(name) { 
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg); //匹配目标参数
  console.log(window.location)
  if (r != null) return unescape(r[2]);
  return null; //返回参数值
}

//获取code   
export function getCodeApi() {
  let urlNow = 'http://xrh5.xjkuang.com/'; //xmk
  // let urlNow = 'http://m-test.xjkuang.com/'; //xmk
  let scope = 'snsapi_base'; //snsapi_userinfo   //静默授权 用户无感知
  let appid = 'wxd9792c70995566a3';
  let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`;
  window.location.replace(url);
}

// 判断是否是微信浏览器
export function isWeixin() {
  return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
}

export const GetQueryString = (name)=>{
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var v = window.location.href;
  if(v.indexOf(name)=='-1'){
    return null;
  } else{
    var r = v.substr(v.indexOf(name)).match(reg);
    if(r != null) return decodeURI(r[2]);
    return null;
  }
};
export const setSessionStorage = (key, value) => {
  if (!key) return;
  let val = value;
  if (typeof value !== 'string') {
    val = JSON.stringify(value);
  }
  window.sessionStorage.setItem(key, val);
}

export const getSessionStorage = (key) => {
  if (!key) return;
  return window.sessionStorage.getItem(key);
}

export const removeSessionStorage = (key) => {
  if (!key) return;
  window.sessionStorage.removeItem(key);
}
